package com.bmps.springcloud.insuredmicroservice.util;

import com.alibaba.fastjson.JSONObject;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * description:
 * xml转换
 *
 * @author jackdaw
 * @date 2019-11-27 17:45
 */
public class XmlUtil {
    private final static String xmlMessage = "<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\"?>\n";
    private final static XStream xStream = new XStream(new DomDriver("GBK"));

    static {
        XStream.setupDefaultSecurity(xStream);
    }

    public static String beanToXml(Object object) {
        xStream.alias("Package", object.getClass());
        xStream.processAnnotations(object.getClass());
        String message = xStream.toXML(object);
        return xmlMessage + message;
    }

    public static Object xmlToBean(Class clazz, String xmlMessage) {
        xStream.alias("Package", clazz);
        xStream.processAnnotations(clazz);

        xStream.allowTypes(new Class[]{clazz});
        return xStream.fromXML(xmlMessage);
    }
}
